import tensorflow as tf
from python_ai.common.xcommon import *

print(tf.__version__)

x = tf.Variable(2.0, name='x')
a = tf.constant(3.0, name='a')
b = tf.constant(5.0, name='b')

with tf.GradientTape() as tape1:
    with tf.GradientTape() as tape2:
        with tf.GradientTape() as tape3:
            tape3.watch([a, b])
            y = x ** a + b
        dydx, dyda, dydb = tape3.gradient(y, [x, a, b])
    dydx2 = tape2.gradient(dydx, x)
dydx3 = tape1.gradient(dydx2, x)
sep('dydx')
tf.print(dydx, output_stream=sys.stdout, summarize=-1)
sep('dydx2')
tf.print(dydx2, output_stream=sys.stdout, summarize=-1)
sep('dydx3')
tf.print(dydx3, output_stream=sys.stdout, summarize=-1)
sep('dyda')
tf.print(dyda, output_stream=sys.stdout, summarize=-1)
sep('dydb')
tf.print(dydb, output_stream=sys.stdout, summarize=-1)
